package com.foreveross.action.weixin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.action.BaseCostroller;
import com.foreveross.common.CommonUtil;
import com.foreveross.service.HrOptimizedService;
import com.foreveross.vo.HrOptimized;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/weixin/hrOptimizedController")
public class HrOptimizedController extends BaseCostroller {

	private Logger log = Logger.getLogger(HrOptimizedController.class);

	@Resource
	HrOptimizedService hrOptimizedService;

	/**
	 * 获取润优选列表
	 * 
	 * @param request
	 * @param response
	 * @param title
	 * @param houseId
	 * @param weixinId
	 * @param currentPage
	 * @param pageSize
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findHrOptimizedList")
	public void findHrOptimizedList(HttpServletRequest request,
			HttpServletResponse response, String title, String houseId,
			String weixinId, String currentPage, String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			int total = (int)hrOptimizedService.getHrOptimizedListCount(weixinId,null,title, houseId);
			PageVo<HrOptimized> pageVo = new PageVo<HrOptimized>(currentPage,total,pageSize);
			List<HrOptimized> list = hrOptimizedService.getHrOptimizedList(weixinId,null,title, houseId,pageVo.getpageIndex(),pageVo.getpageSize());
			
			//图片地址转换
			List<HrOptimized> listOut = new ArrayList<HrOptimized>();
			for (HrOptimized hrOptimized : list){
				List<FileVo> fileVoList = FileVo.getFileVoList(request, hrOptimized.getImage());
				hrOptimized.setOther(fileVoList);
				listOut.add(hrOptimized);
			}
			pageVo.setData(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			log.error("获取润优选列表失败", e);
			resultVo.setMessage("获取润优选列表失败!");
		}  
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,Arrays.asList("publisherId", "createId","optimizedHouses","houseId"));
	}

	/**
	 * 查看润优选详情
	 * 
	 * @param request
	 * @param response
	 * @param hrOptimizedId
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHrOptimizedObj")
	public void getHrOptimizedObj(HttpServletRequest request,
			HttpServletResponse response, String hrOptimizedId) {
		ResultVo resultVo = new ResultVo();
		try {
			HrOptimized hrOptimized = (HrOptimized) this.findObject(
					HrOptimized.class, hrOptimizedId);
			resultVo.setData(hrOptimized);
			resultVo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查看润优选详情失败", e);
			resultVo.setSuccess(false);
			resultVo.setMessage("查看润优选详情失败！");
		}
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, Arrays.asList("publisherId", "createId","optimizedHouses","houseId"));
	}

}
